clear
*set mem 600m
set more off
set logtype text
set matsize 800
cap program drop _all

cap log close

/* Calculate average value of farm property, farm equipment, and livestock in the US for 1850, 1860 and 1870. Use data from
Haines and ICPSR for this.
*/

use $histdir/icpsr_1850_stct.dta, clear
gen val_per_farm = farmval/farms
gen equip_per_farm = equipval/farms
gen lvstk_per_farm = livstock/farms

summ val_per_farm if state==100 & county==0
local val_per_farm_50 = r(mean)
summ equip_per_farm if state==100 & county==0
local equip_per_farm_50 = r(mean)
summ lvstk_per_farm if state==100 & county==0
local lvstk_per_farm_50 = r(mean)

use $histdir/icpsr_1860_stct.dta, clear
gen farms = farm39 + farm1019 + farm2049 + farm5099 + farm100 + farm500 + farm1000
gen val_per_farm = farmval/farms
gen equip_per_farm = equipval/farms
gen lvstk_per_farm = livstock/farms

summ val_per_farm if state==100 & county==0
local val_per_farm_60 = r(mean)
summ equip_per_farm if state==100 & county==0
local equip_per_farm_60 = r(mean)
summ lvstk_per_farm if state==100 & county==0
local lvstk_per_farm_60 = r(mean)

use $histdir/icpsr_1870_stct.dta, clear
gen val_per_farm = farmval/farms
gen equip_per_farm = equipval/farms
gen lvstk_per_farm = livstock/farms

summ val_per_farm if state==100 & county==0
local val_per_farm_70 = r(mean)
summ equip_per_farm if state==100 & county==0
local equip_per_farm_70 = r(mean)
summ lvstk_per_farm if state==100 & county==0
local lvstk_per_farm_70 = r(mean)

*Stack 1850, 1860, and 1870 1% samples on top of one another

use $rawdir/1850_1%.dta, clear
append using $rawdir/1860_1%.dta
append using $rawdir/1870_1%.dta

*Keep white male household heads
keep year region realprop persprop occ1950 occscore  age sex race relate 
keep if sex==1 & race==1 & relate==1

*Create year-specific variable for farm values, equipment values, and livestock values (as calculated at beginning of file)


foreach v in val_per_farm equip_per_farm lvstk_per_farm {
	gen `v' = ``v'_50' if year==1850
	replace `v' = ``v'_60' if year==1860
	replace `v' = ``v'_70' if year==1870
}

gen south = region>30 & region<40
tab south

*CPI data from Minneapolis Fed: http://www.minneapolisfed.org/community_education/teacher/calc/hist1800.cfm

gen cpi = 25 if year==1850
replace cpi = 27 if year==1860
replace cpi = 38 if year==1870

*Deflate all wealth and property value measures using CPI

foreach v in realprop persprop val_per_farm equip_per_farm lvstk_per_farm {
	gen `v'_real = `v'*(25/cpi)
}

/*Adjust farmers' wealth. The reason for this is that much of a farmer's weatlh was used to conduct business, not for
consumption. So this is not exactly a great measure of labour income for farmers. As it turns out, if we don't do this adjustment, 
farmers end up way in the upper tail of the occupational distribution, and we really don't think this is right (for the reasons
just mentioned). So we try to subtract "business related" property from farmers' property, by subtracting the value of farm 
property (from farm owners' wealth, or farmers who report positive real estate wealth) and subtracitng the value of 
equipment and livestock from all farmers' wealth (under the assumption that tenant farmers used their own equipment).
*/



gen realprop_adjust = realprop_real
replace realprop_adjust = realprop_adjust - val_per_farm_real if occ1950==100 & realprop_adjust>0
gen persprop_adjust = persprop_real
replace persprop_adjust = persprop_adjust - equip_per_farm_real - lvstk_per_farm_real if occ1950==100 & persprop_adjust>0
gen persprop_adjust_2 = persprop_real
replace persprop_adjust_2 = persprop_adjust_2 - equip_per_farm_real if occ1950==100 & persprop_adjust>0
gen persprop_xsth = persprop_real if south==0

gen realprop_50 = realprop_real if year == 1850
gen realprop_50adj = realprop_adjust if year == 1850

gen realprop_60 = realprop_real if year == 1860 | year == 1870
gen realprop_60adj = realprop_adjust if year == 1860 | year == 1870

gen logpersprop_real = log(persprop_real + 1)
gen logpersprop_adjust = log(persprop_adjust + 1)
gen logpersprop_adjust2 = log(persprop_adjust_2 + 1)
gen logpersprop_xsth = log(persprop_xsth + 1)

gen logrealprop_real = log(realprop_real + 1)
gen logrealprop_adjust = log(realprop_adjust + 1)

gen logrealprop_50 = log(realprop_50 + 1)
gen logrealprop_50adj = log(realprop_50adj + 1)

gen logrealprop_60 = log(realprop_60 + 1)
gen logrealprop_60adj = log(realprop_60adj + 1)

*Count the number of people who report zero property and who appear in each year

gen real0 = 1 if realprop==0
gen pers0 = 1 if persprop==0
gen year50 = 1 if year==1850
gen year60 = 1 if year==1860
gen year70 = 1 if year==1870

*twoway kdensity logpersprop_real || kdensity logrealprop_real

*Compute averages by occupation

collapse (mean) mean_weal_pers = persprop_real mean_weal_padj = persprop_adjust mean_weal_padj2 = persprop_adjust_2 mean_weal_pxsth = persprop_xsth ///
	mean_logpweal = logpersprop_real mean_logpweal_adj = logpersprop_adjust mean_logpweal_adj2 = logpersprop_adjust2 mean_logpweal_xsth = logpersprop_xsth ///
	mean_weal_real = realprop_real mean_weal_radj = realprop_adjust mean_weal_r50 = realprop_50 mean_weal_r50adj = realprop_50adj ///
	mean_weal_r60 = realprop_60 mean_weal_r60adj = realprop_60adj ///
	(count) number_total=sex number_1850 = year50 number_1860 = year60 number_1870 = year70 number_real0 = real0 number_pers0 = pers0 ///
	(median) med_rweal = realprop_real med_pweal = persprop_real med_occscore = occscore med_age = age, by(occ1950)

*Drop "occupations" that are not really occupations (like "keeping house" or "at home")

keep if occ1950<=970

sort occ1950
save $tempdir/occ_wealth.dta, replace

*Use full count to get more precise measure of 1850 real estate wealth

cd "$ipums100dir"
	unzipfile "1850_100%_IPUMS.zip", replace
	use "1850_100%_IPUMS.dta", clear

*Keep white male household heads
keep year region realprop occ1950 occscore  age sex race relate 
keep if sex==1 & race==1 & relate==1

gen val_per_farm = `val_per_farm_50'

gen realprop_adjust = realprop
replace realprop_adjust = realprop_adjust - val_per_farm if occ1950==100 & realprop_adjust>0

gen logrealprop = log(realprop + 1)
gen logrealprop_adjust = log(realprop_adjust + 1)

collapse (mean) mean_weal_rfull = realprop mean_weal_rfadj = realprop_adjust ///
	mean_logrwfull = logrealprop mean_logrwfull_adj = logrealprop_adjust, by(occ1950)

keep if occ1950 <= 970
sort occ1950

merge 1:1 occ1950 using $tempdir/occ_wealth.dta

sort occ1950

save $tempdir/occ_wealth.dta, replace

erase "$ipums100dir/1850_100%_IPUMS.dta"




